package com.mission.schedule.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.utils.SharedPrefUtil;

@SuppressLint({"SimpleDateFormat", "HandlerLeak"})
public class AddQDListIconActivity extends BaseActivity implements OnClickListener{

    @ViewResId(id = R.id.top_ll_back)
    private LinearLayout top_ll_back;
    @ViewResId(id = R.id.listview)
    private ListView listview;


    Context context;
    App application = App.getDBcApplication();

    String addicon;

    SharedPrefUtil sharedPrefUtil = null;
    ADpater aDpater;
    int po = 0;


    @Override
    protected void setListener() {
        top_ll_back.setOnClickListener(this);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                po = position;
                aDpater.seticon(position);
                sharedPrefUtil.putString(context,ShareFile.USERFILE,ShareFile.ADDQDICON,""+position);
                finish();
            }
        });
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.add_qdlist_icon);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        addicon = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.ADDQDICON, "0");
        po = Integer.valueOf(addicon);
        loadData();
    }

    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case -1:
                    break;
            }
        }
    };

    private void loadData() {
        aDpater = new ADpater();
        listview.setAdapter(aDpater);
        aDpater.seticon(po);
    }


    @Override
    protected void setAdapter() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_ll_back:
                this.finish();
                break;
            default:
                break;
        }
    }


    int[] images = {R.mipmap.icon_beiwang,R.mipmap.icon_putong,R.mipmap.icon_buy,R.mipmap.icon_xinyuan,R.mipmap.icon_renwu};
    String[] names = {"备忘录","清单","购物清单","愿望清单","任务清单"};

    class ADpater extends BaseAdapter{
        int pos = 0;

        public ADpater(){

        }

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MYV myv;
            if (convertView == null){
                myv = new MYV();
                convertView = LayoutInflater.from(context).inflate(R.layout.add_qdlist_icon_item,null);
                myv.icon = (ImageView)convertView.findViewById(R.id.icon);
                myv.set = (ImageView)convertView.findViewById(R.id.set);
                myv.icon_name = (TextView)convertView.findViewById(R.id.icon_name);
                convertView.setTag(myv);
            }else{
                myv = (MYV)convertView.getTag();
            }
            myv.icon.setBackgroundResource(images[position]);
            myv.icon_name.setText(names[position]);
            if (pos == position){
                myv.set.setVisibility(View.VISIBLE);
            }else {
                myv.set.setVisibility(View.GONE);
            }
            return convertView;
        }

        public void seticon(int po){
            this.pos = po;
            notifyDataSetChanged();
        }

        class MYV{
            TextView icon_name;
            ImageView icon,set;
        }
    }
}
